package com.example.demo;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import com.example.demo.exception.BusinessException;
import com.example.demo.exception.ParamException;
import com.example.demo.exception.SystemException;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Qinyi.
 */
@RestControllerAdvice
public class GlobalExceptionAdvice {

    @ExceptionHandler(value = ParamException.class)
    public CommonResponse<String> handlerParamException(HttpServletRequest req,
    		ParamException ex) {
        CommonResponse<String> response = new CommonResponse<>(400,
                "param error");
        response.setData(ex.getMessage());
        return response;
    }
    @ExceptionHandler(value = BusinessException.class)
    public CommonResponse<String> handlerBusinessException(HttpServletRequest req,
    		BusinessException ex) {
        CommonResponse<String> response = new CommonResponse<>(500,
                "business error");
        response.setData(ex.getMessage());
        return response;
    }
    
    @ExceptionHandler(value = SystemException.class)
    public CommonResponse<String> handlerSystemException(HttpServletRequest req,
    		SystemException ex) {
        CommonResponse<String> response = new CommonResponse<>(700,
                "system error");
        response.setData(ex.getMessage());
        return response;
    }
}
